<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 27, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Text\Template
{
	use KLF\System\KObject;
	use KLF\System\Data\Model\KBaseModel;
	use KLF\System\Errors\KFileNotFoundException;
	
	/**
	 *  Class KBaseTemplate
	 *
	 *  Base class for all template type objects in the framework.
	 *
	 *  @package KLF\System\Text\Template
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @method KBaseTemplate NewInstance
	 *  
	 *  @property KBaseModel $Model
	 *  @property string $TemplateFilename
	 */
	class KBaseTemplate extends KObject
	{
		/**
		 * The model object to be used by the template.
		 * 
		 * @var KBaseModel $_propModel
		 */
		protected $_propModel;
		
		/**
		 * The path to the template file.
		 * 
		 * @var string $_propTemplateFile
		 */
		protected $_propTemplateFile;
		
		/**
		 * Constructs a new template instance.
		 * 
		 * @param string $templateFile The full path to the template source.
		 * @param KBaseModel $model The view model for rendering.
		 * @throws KFileNotFoundException
		 */
		public function __construct($templateFile, KBaseModel $model = NULL)
		{
			if (!file_exists($templateFile))
				throw new KFileNotFoundException($templateFile);
			
			$this->_propTemplateFile = $templateFile;
			$this->_propModel = $model;
		}
		
		/**
		* Renders the template directly to the output stream.
		* 
		* @param KBaseModel $model
		 */
		public function Render($model = NULL)
		{
			$model = isset($model) ? $model : $this->_propModel;
			include($this->_propTemplateFile);
		}
		
		/**
		 * Renders the template and returns the content as a string.
		 * 
		 * @return string
		 */
		public function RenderPartial($model = NULL)
		{
			ob_start();
			$this->Render();
			return ob_get_flush();
		}
	}
}